<?php
/**
 * Invitation
 *  
 * @author james
 * @version 
 */
require_once 'lucene/Custom_Search_Lucene_Document.php';
require_once 'Zend/Db/Table/Abstract.php';

class Invitation extends Zend_Db_Table_Abstract {
	
	const LUCENE_INDEX_FOLDER='invitation';
	const PUBLIC_INVITATION = 2;
	const PRIVATE_INVITATION = 1;
	// when invitation is private but it can marked as open
	const OPEN_INVITATION = 3;
	
	/**
	 * The default table name 
	 */
	protected $_name = 'invitation';
	protected $_primary = 'id';
	
	protected $_dependentTables = array ('InvitationMessage', 'InvitationResponse', 'AuthorizationMap', 'InvitationList' )

	;
	
	protected $_referenceMap = array (
		'User' => array (
					'columns' => 'user_id', 
					'refTableClass' => 'User', 
					'refColumns' => 'id' 
					), 
		'Category' => array (
					'columns' => 'category_id', 
					'refTableClass' => 'Category', 
					'refColumns' => 'id' 
					), 
		'Theme' => array (
					'columns' => 'theme_id', 
					'refTableClass' => 'Theme', 
					'refColumns' => 'id' 
					)
		);
	
	function findAllAvailableInvitation() {
		$select = $this->select ();
		$select->from ( $this->_name, array ('id', 'title' ) );
		$rows = $this->fetchAll ( $select );
		return $rows->toArray ();
	}
	
	/**
	 * Find all Invitations.
	 */
	function findAll() {
		return $this->fetchAll($this->select());
	}
	
	/**
	 * Find invitations created by given user, sorted by modified date.
	 * 
	 * @param $user_id
	 * @return return list of invitations
	 */
	function findInvitationByUser($user_id) {
		$select = $this->select ();
		$select->where ( 'user_id = ?', $user_id );
		$select->order ( 'modified DESC' );
		$rows = $this->fetchAll ( $select );
		return $rows;
	}
	
	/**
	 * Find invitation by id.
	 * 
	 * @param $id
	 */
	public static function findById($id) {
		$invitation = new Invitation ( );
		return $invitation->find ( $id )->current ();
	}
	
	/**
	 * Find invitation by unique criteria.
	 * 
	 * @param $user_id
	 * @param $category
	 * @param $resource
	 * @param $title
	 */
	public static function findUnique($user_id, $category, $resource, $title) {
		$invitation = new Invitation ( );
		$select = $invitation->select ()->where ( 'user_id = ?', $user_id )->where ( 'category_id = ?', $category )->where ( 'resource_id = ?', $resource )->where ( 'title = ?', $title );
		return $invitation->fetchRow ( $select );
	}
	
	/**
	 * Save or update invitation.
	 * 
	 * @param $data
	 * @return return created/updated invitation
	 */
	function saveOrUpdateInvitation($data) {
		//		$data = Invitation::findUnique($user_id, $category, $theme, $title);
		$id = $data['id'];
		$invitation = Invitation::findById ( $id );
		if (isset ( $invitation )) {
			if (isset($data['category_id'])) {
				$invitation->category_id = $data['category_id'];
			}
			if (isset($data['theme_id'])) {
				$invitation->theme_id = $data['theme_id'];
			}
			if (isset($data['date'])) {
				$invitation->date = $data['date'];
			}
			if (isset($data['description'])) {
				$invitation->description = $data['description'];
			}
			if (isset($data['address'])) {
				$invitation->address = $data['address'];
			}
			if (isset($data['modified'])) {
				$invitation->modified = $data['modified'];
			}
			if (isset($data['status'])) {
				$invitation->status = $data['status'];
			}
			if (isset($data['permission_id'])) {
				$invitation->permission_id = $data['permission_id'];
			}
			if (isset($data['open'])) {
				$invitation->open = $data['open'];
			}
			$invitation->save ();
		} else {
			$invitation = Invitation::findById($this->insert($data));
		}
		return $invitation;
	}
	
	/**
	 * Delete invitation by id.
	 * 
	 * @param $id
	 */
	function deleteById($id) {
		$this->delete ( "id = " . $id );
	}
	
	/**
	 * Find all comments by given invitation id.
	 * 
	 * @param $id invitation id
	 */
	function findAllComment($id) {
		$invitation = new Invitation ( );
		$invitation = $this->findById ( $id );
		$messages = $invitation->findManyToManyRowset ( 'Message', 'InvitationMessage', 'Invitation', null, $this->select ()->order ( 'created DESC' ) );
		return $messages;
	}
	
	/**
	 * Find invitation authorization send out status,
	 * How many people confirmed and how many people didn't visit.
	 * 
	 * @param $id invitation id
	 * @param $status status
	 */
	function findInvitationSendOutStatus($id, $status) {
		$invitation = new Invitation ( );
		$invitation = $this->findById ( $id );
		$invitation->findDependentRowset ( 'AuthorizationMap', null, $invitation->select ()->where ( 'status=?', $status ) );
	}
	
	/**
	 * Find invitation response by given condition.
	 * 
	 * @param $id invitation id
	 * @param $select
	 */
	function findInvitationResponse($id, $select) {
		$invitation = $this->findById ( $id );
		return $invitation->findDependentRowset ( 'InvitationResponse', 'Invitation', $select );
	}
	
	/**
	 * Find user by given invitation.
	 * 
	 * @param $invitation_id
	 */
	public static function getUser($invitation) {
		return $invitation->findParentRow ( 'User' );
	}
	
	/**
	 * Find theme of given invitation.
	 * 
	 * @param $invitation
	 */
	public static function getTheme($invitation) {
		return $invitation->findParentRow ( 'Theme' );
	}
	
	/**
	 * Find category of given invitation.
	 * 
	 * @param $invitation
	 */
	public static function getCategory($invitation) {
		return $invitation->findParentRow ( 'Category' );
	}
	
	/**
	 * Get invitation list, listed by status.
	 * 
	 * @param $invitation
	 * @return array with status as key and list of user objects as value
	 */
	public static function getInvitedUsers($invitation) {
		$invitationList = $invitation->findDependentRowset ( 'InvitationList', 'Invitation' );
		$invitedUser = array ('active' => array (), 'pending' => array (), 'other' => array () );
		foreach ( $invitationList as $entry ) {
			$user = User::findById ( $entry->invited_user_id );
			if ($entry->status == 'active') {
				$invitedUser ['active'] [$entry->id] = $user;
			} else if ($entry->status == 'pending') {
				$invitedUser ['pending'] [$entry->id] = $user;
			} else {
				$invitedUser ['other'] [$entry->id] = $user;
			}
		}
		return $invitedUser;
	}
	
	/**
	 * Find resouce by given invitation and resource type.
	 * 
	 * @param $invitation invitation object
	 * @param $resource_type resource type
	 */
	public static function findResourceByInvitationAndResourceType($invitation, $resource_type) {
		$theme = Invitation::getTheme ( $invitation );
	}
	
	/**
	 * Get lucene search fields.
	 * 
	 * @param $invitation invitation
	 */
	public static function _getSearchIndexFields($invitation) {
		$fields = array();
		$fields['class'] = 'Invitation';
		$fields['key'] = $invitation->id;
		$fields['content'] = $invitation->title;
		$fields['description'] = $invitation->description;
		$fields['date'] = $invitation->date;
		$fields['created'] = $invitation->created;
		return $fields;
	}
	
	/**
	 * Add invitation object to lucene index.
	 * 
	 * @param $invitation_id
	 */
	public function addToIndex($invitation_id) {
		$invitation = Invitation::findById($invitation_id);
		$index = Zend_Search_Lucene::open('/opt/lucene/index/');
		$doc = new Custom_Search_Lucene_Document('Invitation', $invitation->id, $invitation->title, $invitation->description);
		$index->addDocument($doc);
	}
}

